﻿using System.Security.Cryptography.Pkcs;
using DFGK.IService.BBS;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DFGK_WMS.WebAPI.Controllers.Platform
{
    /// <summary>
    /// 共享文件类别
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShareFileTypeController : BaseController
    {
        private readonly IShareFileTypeService _shareFileTypeService;

        public ShareFileTypeController(IShareFileTypeService shareFileTypeService,IWebHostEnvironment webEnvironment):base(webEnvironment)
        {
            _shareFileTypeService = shareFileTypeService;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="typeName">类别名称</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPageData(string? typeName, int pageIndex=1, int pageSize=50)
        {
            int totalCount = 0;
            var page = _shareFileTypeService.GetShareFileTypePageData(typeName, out totalCount, pageIndex, pageSize);
            return Ok(BaseResult.Result(page, "查询成功！", "200", totalCount));

        }

        /// <summary>
        /// 新增/修改公告类别
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitShareFileType(ShareFileTypeInfo model)
        {
            if (model.Id > 0)
            {
                model.ModifyDate = DateTime.Now;
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
            }
            else
            {
                model.CreateUserName = base.BaseGetNowUserName();
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateDate = DateTime.Now;
                model.FileCount = 0;
                model.FileOnlineCount = 0;
                model.FileUnlineCount = 0;
            }
            bool result = _shareFileTypeService.SubmitShareFileType(model, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// ID查询共享文件类别
        /// </summary>
        /// <param name="shareFileTypeId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShareFileType(long shareFileTypeId)
        {
            var cobj = _shareFileTypeService.GetShareFileInfo(shareFileTypeId);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }
         
        /// <summary>
        /// 删除共享文件类别
        /// </summary>
        /// <param name="shareFileTypeId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelShareFileType(long shareFileTypeId)
        {
            var result = _shareFileTypeService.DelShareFileType(shareFileTypeId, out string errorMsg);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！" + errorMsg, "500"));
        }
    }
}
